<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import { fetchList, fetchPv } from '@/api/ApiService'
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      data_xAxis:['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      data_series:[79, 52, 200, 334, 390, 330, 220],
      listQuery: {
      //  page: 1,
      //  limit: 20,
      // importance: undefined,
      //  title: undefined,
      //  type: undefined,
      //  sort: '+id'
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getList()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery.ActionCode ='SearchStockPrepareInTimeReport';
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.data_xAxis = []
        this.data_series = []
        var data = {};
        this.list.forEach((item,index,array)=>{
          if(data[item["物料代码"]]){
            data[item["物料代码"]] += item["数量"]
          }else{
            data[item["物料代码"]] = item["数量"]
          }           
        })
        var i = 0;
        for(var key in data){
          i++;
          if(i<10){
            this.data_xAxis.push(key);
            this.data_series.push(data[key]);
          }
           
        }

        this.initChart()

        this.listLoading = false
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'line' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.data_xAxis,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
                interval:0 
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '数量',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: this.data_series,
          animationDuration
        }]
      })
    }
  }
}
</script>
